package com.ruoyi.web.a2.service.impl;


import com.ruoyi.common.config.DbInterfaceConfig;
import com.ruoyi.common.enums.CleanStatusEnum;
import com.ruoyi.common.enums.CommonOKNOEnum;
import com.ruoyi.common.enums.FlightPathTypeEnum;
import com.ruoyi.common.utils.Date2Utils;
import com.ruoyi.common.utils.crudinterface.CrudInterfaceUtils;
import com.ruoyi.common.utils.crudinterface.vo.ReqVo;
import com.ruoyi.web.a2.domain.JKySAdsdtxx;
import com.ruoyi.web.a2.domain.JKySAisdtxx;
import com.ruoyi.web.a2.dto.PointCleanJson;
import com.ruoyi.web.a2.service.ITool2aSpaceTimeResampleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Slf4j
@Service
public class Tool2aSpaceTimeResampleImpl implements ITool2aSpaceTimeResampleService
{

    @Autowired
    private DbInterfaceConfig dbInterfaceConfig;

    private static final String STATUS_NAME = "sfwcskccy";

    /**
     *  时空重采样
     *  实现思路如下：
     * （1）循环所有点位，与数据库已有数据进行比对去重，唯一值：MMSI+基准时间+经度+纬度
     *
     * @author :yanlixiong
     * @date :2025-02-17 09:28:54
     */
    @Override
    public List<JKySAisdtxx> handleAIS(PointCleanJson json) {
        FlightPathTypeEnum flightPathTypeEnum = FlightPathTypeEnum.JC;
        //先查询AIS/ADS-B动态信息
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        Object dbList = crudInterfaceUtils.getList(dbInterfaceConfig, new ReqVo(this.getQuerySql(flightPathTypeEnum,json)), JKySAisdtxx.class);
        List<JKySAisdtxx> list = (List<JKySAisdtxx>) dbList;
        if (!CollectionUtils.isEmpty(list)) {
            for(JKySAisdtxx info:list){
                String baseTimeStr = Date2Utils.timestampToDateTimeStr(info.getJzsj());

                StringBuilder sql = new StringBuilder();
                sql.append("SELECT id,qxzt FROM ")
                        .append(flightPathTypeEnum.getDynamicsTableName())
                        .append(" WHERE ")
                        .append(flightPathTypeEnum.getMmsiIcao())
                        .append(" = '")
                        .append(info.getMmsi()).append("'")
                        .append(" AND jzsj ='")
                        .append(baseTimeStr).append("'")
                        .append(" AND jd =")
                        .append(info.getJd())
                        .append(" AND wd =")
                        .append(info.getWd());
                Object dbBean = crudInterfaceUtils.getList(dbInterfaceConfig,new ReqVo(sql.toString()), JKySAisdtxx.class);
                List<JKySAisdtxx> listDB = (List<JKySAisdtxx>)dbBean;

                if(!CollectionUtils.isEmpty(listDB)){
                    info.setQxzt(CleanStatusEnum.DATA_3.getCode());
                }

                info.setSfwcskccy(CommonOKNOEnum.OK.getCode());
                String templateSql = "UPDATE %s SET %s = %d , qxzt = %d WHERE id = %d";
                String sql2 = String.format(templateSql, flightPathTypeEnum.getDynamicsTableName(),STATUS_NAME,info.getSfwcskccy(), info.getQxzt(),info.getId());

                crudInterfaceUtils.update(dbInterfaceConfig,new ReqVo(sql2));
            }
        }
        return list;
    }

    @Override
    public List<JKySAdsdtxx> handleADS(PointCleanJson json) {
        FlightPathTypeEnum flightPathTypeEnum = FlightPathTypeEnum.FJ;
        //先查询AIS/ADS-B动态信息
        CrudInterfaceUtils crudInterfaceUtils = new CrudInterfaceUtils();
        Object dbList = crudInterfaceUtils.getList(dbInterfaceConfig, new ReqVo(this.getQuerySql(flightPathTypeEnum,json)), JKySAdsdtxx.class);
        List<JKySAdsdtxx> list = (List<JKySAdsdtxx>) dbList;
        if (!CollectionUtils.isEmpty(list)) {
            for(JKySAdsdtxx info:list){
                String baseTimeStr = Date2Utils.timestampToDateTimeStr(info.getJzsj());

                StringBuilder sql = new StringBuilder();
                sql.append("SELECT id,qxzt FROM ")
                        .append(flightPathTypeEnum.getDynamicsTableName())
                        .append(" WHERE ")
                        .append(flightPathTypeEnum.getMmsiIcao())
                        .append(" = '")
                        .append(info.getIcao()).append("'")
                        .append(" AND jzsj ='")
                        .append(baseTimeStr).append("'")
                        .append(" AND jd =")
                        .append(info.getJd())
                        .append(" AND wd =")
                        .append(info.getWd());
                Object dbBean = crudInterfaceUtils.getList(dbInterfaceConfig,new ReqVo(sql.toString()), JKySAdsdtxx.class);
                List<JKySAdsdtxx> listDB = (List<JKySAdsdtxx>)dbBean;

                if(!CollectionUtils.isEmpty(listDB)){
                    info.setQxzt(CleanStatusEnum.DATA_3.getCode());
                }

                info.setSfwcskccy(CommonOKNOEnum.OK.getCode());
                String templateSql = "UPDATE %s SET %s = %d , qxzt = %d WHERE id = %d";
                String sql2 = String.format(templateSql, flightPathTypeEnum.getDynamicsTableName(),STATUS_NAME,info.getSfwcskccy(), info.getQxzt(),info.getId());

                crudInterfaceUtils.update(dbInterfaceConfig,new ReqVo(sql2));
            }
        }
        return list;
    }


    private String getQuerySql(FlightPathTypeEnum flightPathTypeEnum,PointCleanJson json){
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ")
                .append(flightPathTypeEnum.getDynamicsField())
                .append(" FROM ")
                .append(flightPathTypeEnum.getDynamicsTableName())
                .append(" WHERE ")
                .append(STATUS_NAME)
                .append(" = ")
                .append(CommonOKNOEnum.NO.getCode());
        if (StringUtils.isNotBlank(json.getStartTime())) {
            sql.append(" AND jzsj >='").append(json.getStartTime()).append("'");
        }
        if (StringUtils.isNotBlank(json.getEndTime())) {
            sql.append(" AND jzsj <='").append(json.getEndTime()).append("'");
        }
        if (StringUtils.isNotBlank(json.getTargetNum())) {
            sql.append(" AND ").append(flightPathTypeEnum.getMmsiIcao()).append(" ='").append(json.getTargetNum()).append("'");
        }
        sql.append(" ORDER BY jzsj");
        return sql.toString();
    }

}
